package com.wofish.goods.service.impl;

import com.wofish.common.core.text.Convert;
import com.wofish.goods.domain.Goods;
import com.wofish.goods.domain.GoodsSpecIndex;
import com.wofish.goods.domain.Products;
import com.wofish.goods.dto.ProductsIndex;
import com.wofish.goods.dto.SpecObject;
import com.wofish.goods.mapper.ProductsMapper;
import com.wofish.goods.service.IGoodsService;
import com.wofish.goods.service.IGoodsSpecIndexService;
import com.wofish.goods.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 货品 服务层实现
 * 
 * @author wofish
 * @date 2019-06-16
 */
@Service
public class ProductsServiceImpl implements IProductsService {
	@Autowired
	private ProductsMapper productsMapper;

	@Autowired
	private IGoodsSpecIndexService goodsSpecIndexService;

	@Autowired
	private IGoodsService goodsService;

	/**
     * 查询货品信息
     * 
     * @param productId 货品ID
     * @return 货品信息
     */
    @Override
	public Products selectProductsById(Long productId) {
	    return productsMapper.selectProductsById(productId);
	}
	
	/**
     * 查询货品列表
     * 
     * @param products 货品信息
     * @return 货品集合
     */
	@Override
	public List<Products> selectProductsList(Products products) {
	    return productsMapper.selectProductsList(products);
	}
	
    /**
     * 新增货品
     * 
     * @param products 货品信息
     * @return 结果
     */
	@Override
	public int insertProducts(Products products) {
	    return productsMapper.insertProducts(products);
	}

	@Override
	public int insertBatchProducts(List<Products> list) {
		return productsMapper.insertBatchProducts(list);
	}

	/**
     * 修改货品
     * 
     * @param products 货品信息
     * @return 结果
     */
	@Override
	public int updateProducts(Products products) {
	    return productsMapper.updateProducts(products);
	}

	/**
     * 删除货品对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteProductsByIds(String ids) {
		return productsMapper.deleteProductsByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<Products> queryProductByGoodsId(Long goodsId) {
		return productsMapper.queryProductByGoodsId(goodsId);
	}

	@Override
	public List<String> queryProductsByBns(List<Products> list, Long goodsId) {
		return productsMapper.queryProductsByBns(list, goodsId);
	}

	@Override
	public boolean editProductsIndex(List<Products> productsList, List<ProductsIndex> productsIndexList, Long goodsId,
	                                 Long typeId) {
		boolean result = true;
		try {
			//保存之前删除之前的
			goodsSpecIndexService.deleteGoodsSpecIndexByGoodsId(goodsId);
			productsMapper.deleteProductByGoodsId(goodsId);

			Goods oldGoods = goodsService.selectGoodsById(goodsId);
			boolean goodsMarketable = false;//下架
			for (Products products : productsList) {
				products.setGoodsId(goodsId);
				products.setName(oldGoods.getName());
				products.setGoodsType("normal");
				products.setDisabled(0);
				if(products.getFreez() == null) {
					products.setFreez(0);
				}

				if(products.getMarketable()){
					goodsMarketable = true;
				}
			}

			//操作货品上下架需要更新关联的商品上下架状态
			//1.当所有货品都下架，对应的商品也需同时下架
			//2.当所有货品任意一个或以上上架，对应的商品也需同时上架
			goodsService.updateMarketable(goodsId,goodsMarketable);
			productsMapper.insertBatchProducts(productsList);

			//商品规格值
			List<GoodsSpecIndex> goodsSpecIndices = goodsSpecIndexService.buildGoodsSpecIndexes(goodsId, typeId, productsIndexList, productsList);
			goodsSpecIndexService.insertBatch(goodsSpecIndices);
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}

		return result;
	}

}
